<?php
namespace app\farm\controller;
use app\AdminRest;
use app\farm\model\ClaimOrder;
use app\farm\model\Farmer;
use app\farm\model\FinanceWater;
use app\farm\model\LandOrder;
use app\farm\model\ShopOrder;
use app\farm\model\User;
use app\farm\model\Wallet;
use app\shop\model\DistributionCash;
use app\shop\model\DistributionList;
use longbingcore\wxcore\Excel;
use think\App;
use app\shop\model\Order as Model;


class AdminExcel extends AdminRest
{


    protected $model;

    protected $order_goods_model;

    protected $refund_order_model;

    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new LandOrder();


    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-15 14:43
     * @功能说明:列表
     */
    public function landOrderList(){

        $input = $this->_param;

        $order_model  = new LandOrder();

        $farmer_model = new Farmer();

        $user_model   = new User();
        //初始化订单
        $order_model->orderInit();

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];

        }

        if(!empty($input['goods_name'])){

            $dis[] = ['a.goods_name','like','%'.$input['goods_name'].'%'];

        }

        if(!empty($input['mobile'])){

            $dis[] = ['b.mobile','like','%'.$input['mobile'].'%'];

        }

        if(!empty($input['farmer_id'])){

            $dis[] = ['a.farmer_id','=',$input['farmer_id']];

        }


        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['a.create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        $data = $order_model->adminDataSelect($dis);

        if(!empty($data)){

            foreach ($data as &$vs){

                $vs['farmer_info'] = $farmer_model->dataInfo(['id'=>$vs['farmer_id']],'title');

                $vs['user_info']   = $user_model->dataInfo(['id'=>$vs['user_id']]);

            }
        }

        $name  = '土地订单列表';

        $header=[
            '订单ID',
            '商品名',
            '到期时间',
            '面积',
            '农场名称',
            '服务类型',
            '租赁人',
            '租赁人手机号',
            '下单人',
            '下单人手机号',
            '实收金额',
            '系统订单号',
            '付款订单号',
            '下单时间',
            '支付方式',
            '状态',
        ];

        $new_data = [];

        foreach ($data as $v){

            $info   = array();

            $info[] = $v['id'];

            $info[] = $v['goods_name'];

            $info[] = date('Y-m-d H:i:s',$v['end_time']);

            $info[] = $v['area'].'m²';

            $info[] = !empty($v['farmer_info']['title'])?$v['farmer_info']['title']:'';

            $info[] = $v['massif_title'];

            $info[] = $v['rent_user_name'];

            $info[] = $v['rent_mobile'];

            $info[] = $v['user_name'];

            $info[] = $v['mobile'];

            $info[] = $v['pay_price'];

            $info[] = $v['order_code'];

            $info[] = $v['transaction_id'];

            $info[] = $v['create_time']?date('Y-m-d H:i:s',$v['create_time']):'暂无信息';

            $info[] = !empty($v['balance'])&&$v['balance']>0?'余额支付':'微信支付';

            $info[] = $this->orderStatusText($v['pay_type']);


            $new_data[] = $info;
        }

        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data);

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-12-28 11:26
     * @功能说明:用户下单列表
     */
    public function claimOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];

        }

        if(!empty($input['goods_name'])){

            $dis[] = ['a.goods_name','like','%'.$input['goods_name'].'%'];

        }

        if(!empty($input['mobile'])){

            $dis[] = ['b.mobile','like','%'.$input['mobile'].'%'];

        }

        if(!empty($input['farmer_id'])){

            $dis[] = ['a.farmer_id','=',$input['farmer_id']];

        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['a.create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        $order_model  = new ClaimOrder();

        $farmer_model = new Farmer();

        $user_model   = new User();

        $data = $order_model->adminDataSelect($dis);

        if(!empty($data)){

            foreach ($data as &$vs){

                $vs['farmer_info'] = $farmer_model->dataInfo(['id'=>$vs['farmer_id']],'title');

                $vs['user_info']   = $user_model->dataInfo(['id'=>$vs['user_id']]);

            }
        }


        $name  = '认养订单列表';

        $header=[
            '订单ID',
            '商品名',
            '数量',
            '配送周期',
            '农场名称',
            '认养编号',
            '下单人',
            '下单人手机号',
            '实收金额',
            '系统订单号',
            '付款订单号',
            '下单时间',
            '支付方式',
            '配送方式',
            '状态',
        ];

        $new_data = [];

        foreach ($data as $v){

            $info   = array();

            $info[] = $v['id'];

            $info[] = $v['goods_name'];

            $info[] = $v['num'];

            $info[] = $v['send_cycle'].'共'.$v['send_times'].'次';

            $info[] = $v['farmer_info']['title'];

            $info[] = $v['claim_code'];

            $info[] = $v['user_name'];

            $info[] = $v['mobile'];

            $info[] = $v['pay_price'];

            $info[] = $v['order_code'];

            $info[] = $v['transaction_id'];

            $info[] = $v['create_time']?date('Y-m-d H:i:s',$v['create_time']):'暂无信息';

            $info[] = !empty($v['balance'])&&$v['balance']>0?'余额支付':'微信支付';

            $info[] = $v['send_type']==1?'自提':'快递';

            $info[] = $this->orderStatusText($v['pay_type'],2);

            $new_data[] = $info;
        }

        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-03-08 14:55
     * @功能说明:商城订单导出
     */
    public function shopOrderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['b.goods_name','like','%'.$input['name'].'%'];

            $where[] = ['a.order_code','like','%'.$input['name'].'%'];

        }

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

        }

        if(!empty($input['store_id'])){

            $dis[] = ['a.store_id','=',$input['store_id']];

        }

        $order_model = new ShopOrder();

        $data = $order_model->adminDataSelect($dis,$where);


        return $this->success($data);



    }




    /**
     * @author chenniang
     * @DataTime: 2021-03-30 16:32
     * @功能说明:
     */
    public function orderStatusText($status,$type=1){

        switch ($status){

            case 1:
                return '待支付';

                break;
            case 2:
                return $type==1?'租赁中':'认养中';

                break;
            case 3:
                return '配送中';

                break;

            case 7:
                return '已完成';

                break;

            case -1:
                return '已取消';

                break;

        }

    }



    /**
     * @author chenniang
     * @DataTime: 2021-03-18 13:37
     * @功能说明:财务数据统计导出
     */
    public function dateCount(){

        $input = $this->_param;

        $water_model = new FinanceWater();

        if($input['type']==1){

            $where[] = ['farmer_id','=',$input['farmer_id']];

        }else{

            $where[] = ['store_id','=',$input['farmer_id']];
        }

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $where[] = ['create_time','between',"{$input['start_time']},{$input['end_time']}"];
        }

//        dump($where);exit;

        $data = $water_model->where($where)->field('create_date')->group('create_date')->select()->toArray();

        if(!empty($data)){

            foreach ($data as &$vs){
                //查询各个类型的单日的流水除去余额
                $date_water = $water_model->getDateList($input['farmer_id'],$vs['create_date'],$input['type']);

                $vs = array_merge($vs,$date_water);
                //查询当前余额
                $day_water = $water_model->getDayCash($input['farmer_id'],$input['type'],$vs['create_date']);

                $vs['cash'] = $day_water['cash'];

            }

        }
        $farmer_model = new Farmer();

        $farmer_name = $farmer_model->where(['id'=>$input['farmer_id']])->value('title');

        $name  = $farmer_name.'账务列表';

        $header=[
            '收支时间',
            '土地订单收入(元)',
            '土地配送收入(元)',
            '认养订单收入(元)',
            '认养配送收入(元)',
            '养殖订单收入(元)',
            '提现(元)',
            '余额(元)',
        ];

        $new_data = [];

        foreach ($data as $v){

            $info   = array();

            $info[] = $v['create_date'];

            $info[] = $v['land_cash'];

            $info[] = $v['land_send_cash'];

            $info[] = $v['claim_cash'];

            $info[] = $v['claim_send_cash'];

            $info[] = $v['breed_cash'];

            $info[] = $v['wallet_cash'];

            $info[] = $v['cash'];

            $new_data[] = $info;
        }

        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data);

        return $this->success($data);

    }




    /**
     * @author chenniang
     * @DataTime: 2022-07-29 14:48
     * @功能说明:用户收益列表
     */
    public function userProfitListExcel(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['start_time'])&&!empty($input['end_time'])){

            $dis[] = ['a.create_time','between',"{$input['start_time']},{$input['end_time']}"];

        }

        $where = [];

        if(!empty($input['name'])){

            $where[] = ['a.nickName','like','%'.$input['name'].'%'];

            $where[] = ['b.user_name','like','%'.$input['name'].'%'];

        }

        $fx_model   = new DistributionList();

        $cash_model = new DistributionCash();

        $wallet_model = new Wallet();

        $data = $fx_model->userProfitSelect($dis,$where);

        if(!empty($data)){

            foreach ($data as &$v){
                //产生收益
                $v['total_fx_cash']      = $cash_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('cash');
                //未入账
                $v['unrecorded_fx_cash'] = $cash_model->where(['user_id'=>$v['id']])->where('status','in',[1])->sum('cash');
                //累计提现
                $v['total_wallte_cash']  = $wallet_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('pay_price');
                //提现中
                $v['wallte_ing_cash']    = $wallet_model->where(['user_id'=>$v['id']])->where('status','in',[1])->sum('pay_price');

            }

        }

        $name  = '收益信息列表';

        $header=[
            'ID',
            '姓名',
            '微信昵称',
            '总收益',
            '总提现',
            '提现中',
            '未入账',
            '当前余额',
        ];

        $new_data = [];

        if(!empty($data)){

            foreach ($data as $v){

                $info   = array();

                $info[] = $v['id'];

                $info[] = !empty($v['user_name'])?$v['user_name']:'';

                $info[] = $v['nickName'];

                $info[] = $v['total_fx_cash'];

                $info[] = $v['total_wallte_cash'];

                $info[] = $v['wallte_ing_cash'];

                $info[] = $v['unrecorded_fx_cash'];

                $info[] = $v['fx_cash'];

                $new_data[] = $info;
            }
        }

        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data);

        return $this->success($data);
    }




    /**
     * @author chenniang
     * @DataTime: 2022-07-29 10:17
     * @功能说明:分销商列表导出
     */
    public function userRelationshipListExcel(){

        $input = $this->_param;

        $dis = [

            'uniacid' => $this->_uniacid,

            'is_fx'   => 1
        ];

        $user_model = new User();

        $cash_model = new DistributionCash();

        $wallet_model = new Wallet();

        $data = $user_model->where($dis)->field('id,avatarUrl,nickName,pid,fx_bind_time,fx_cash')->order('fx_bind_time')->select()->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $v['top_name']      = $user_model->where(['id'=>$v['pid'],'is_fx'=>1])->value('nickName');
                //产生收益
                $v['total_fx_cash'] = $cash_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('cash');
                //累计提现
                $v['wallte_cash']   = $wallet_model->where(['user_id'=>$v['id']])->where('status','in',[1,2])->sum('pay_price');

            }

        }

        $name  = '分销商列表';

        $header=[
            'ID',
            '微信昵称',
            '产生收益(元)',
            '总收益(元)',
            '已提现(元)',
            '上线id',
            '上线昵称',
            '创建时间',
        ];

        $new_data = [];

        if(!empty($data)){

            foreach ($data as $v){

                $info   = array();

                $info[] = $v['id'];

                $info[] = $v['nickName'];

                $info[] = $v['total_fx_cash'];

                $info[] = $v['fx_cash'];

                $info[] = $v['wallte_cash'];

                $info[] = $v['pid'];

                $info[] = !empty($v['top_name']);

                $info[] = date('Y-m-d H:i:s',$v['fx_bind_time']);

                $new_data[] = $info;
            }
        }

        $excel = new Excel();

        $excel->excelExport($name,$header,$new_data);

        return $this->success($data);

    }







}
